#!/usr/bin/python2.3
from __future__ import division
import visual, visual.controls
import random, archdesc, arch, copy

class apanel:
  def __init__(self, display, text, pos, width, height):
    self.l = visual.label(pos=(pos[0]+60, pos[1]+12), text=text, box=0, display=display)
    self.c = visual.curve(pos=[pos, (pos[0], pos[1]-height), (pos[0]+width, pos[1]-height), (pos[0]+width, pos[1]), pos, (pos[0], pos[1]+24), (pos[0]+120, pos[1]+24), (pos[0]+120, pos[1])], display=display)

class aslider:
  def __init__(self, display, text, pos, length, values, action=None):
    self.values = copy.deepcopy(values)
    self.action = action

    visual.label(pos=(pos[0]-120, pos[1]), text=text, box=0, display=display, opacity=0)
    self.label = visual.label(pos=(pos[0]-60, pos[1]), text=str(values[0]), display=display, box=0, opacity=0)
    self.slider = visual.controls.slider(pos=pos, length=length, axis=(1, 0), min=0, max=len(values)-1)
    self.slider.action = lambda: self.update()

  def update(self):
    self.label.text = str(self.values[int(self.slider.value)])
    if self.action:
      self.action()

class network:
  def __init__(self, display, text, pos):
    self.components = []
    self.panel = apanel(display, text, pos, 350, 50)
    self.type = aslider(display, 'Type', (pos[0]+140, pos[1]-15), 200, ['bus', 'uniring', 'fc'])
    self.bandwidth = aslider(display, 'B/W', (pos[0]+140, pos[1]-35), 200, [1000000, 2500000, 5000000, 10000000, 25000000, 50000000, 100000000, 250000000], action=lambda: self.update())

  def update(self):
    for c in self.components:
      c.bandwidth.slider.value = self.bandwidth.slider.value
  
class component:
  def __init__(self, display, text, pos):
    self.panel = apanel(display, text, pos, 350, 50)
    self.type = aslider(display, 'Type', (pos[0]+140, pos[1]-15), 200, ['none', 'm24kc', 'ss', 'tm', 'im66', 'im132', 'xl', 'xl160', 'xl640', 'rc2', 'rc4'])
    self.bandwidth = aslider(display, 'B/W', (pos[0]+140, pos[1]-35), 200, [1000000, 2500000, 5000000, 10000000, 25000000, 50000000, 100000000, 250000000])   

class mapper:
  def __init__(self, display, text, pos):
    self.panel = apanel(display, text, pos, 350, 110)
    self.maxtime = aslider(display, 'L_inf', (pos[0]+140, pos[1]-15), 200, [0.0, 0.25, 0.5, 0.75, 1])
    self.avgtime = aslider(display, 'L_1', (pos[0]+140, pos[1]-35), 200, [0.0, 0.25, 0.5, 0.75, 1])
    self.avgsqtime = aslider(display, 'L_2', (pos[0]+140, pos[1]-55), 200, [0.0, 0.25, 0.5, 0.75, 1])
    self.avgenergy = aslider(display, 'E', (pos[0]+140, pos[1]-75), 200, [0.0, 0.25, 0.5, 0.75, 1])
    self.avgresources = aslider(display, 'Res', (pos[0]+140, pos[1]-95), 200, [0.0, 0.25, 0.5, 0.75, 1])

class archdescui:
  def __init__(self, display, pos):
    self.network = network(display, 'Network', pos)
    c1 = component(display, 'Component 1', (pos[0], pos[1]-80))
    c2 = component(display, 'Component 2', (pos[0], pos[1]-2*80))
    c3 = component(display, 'Component 3', (pos[0], pos[1]-3*80))
    c4 = component(display, 'Component 4', (pos[0], pos[1]-4*80))
    c5 = component(display, 'Component 5', (pos[0], pos[1]-5*80))
    self.network.components = [c1, c2, c3, c4, c5]
    self.mapper = mapper(display, 'Mapper', (pos[0], pos[1]-6*80))

  def set(self, a):
    if isinstance(a, basestring):
      a = arch.ReadSystem(a)
    if isinstance(a, arch.System):
      a = archdesc.BuildDescription(a)
      a.archdesc.static = 1
  
    for i in range(len(self.network.type.values)):
      if a.archdesc.type == self.network.type.values[i]:
        self.network.type.slider.value = i

    for i in range(len(self.network.bandwidth.values)):
      if a.archdesc.bandwidth == self.network.bandwidth.values[i]:
        self.network.bandwidth.slider.value = i

    for j in range(len(self.network.components)):
      c = self.network.components[j]
      if j < len(a.archdesc.components) - a.archdesc.static:
        ac = a.archdesc.components[j+a.archdesc.static]
      
        for i in range(len(c.type.values)):
          if ac.type == c.type.values[i]:
            c.type.slider.value = i
        for i in range(len(c.bandwidth.values)):
          if ac.bandwidth == c.bandwidth.values[i]:
            c.bandwidth.slider.value = i
      else:
        c.type.slider.value = 0
        c.bandwidth.slider.value = 0
        
    for i in range(len(self.mapper.maxtime.values)):
      if a.mapdesc.maxtime == self.mapper.maxtime.values[i]:
        self.mapper.maxtime.slider.value = i

    for i in range(len(self.mapper.avgtime.values)):
      if a.mapdesc.avgtime == self.mapper.avgtime.values[i]:
        self.mapper.avgtime.slider.value = i

    for i in range(len(self.mapper.avgsqtime.values)):
      if a.mapdesc.avgsqtime == self.mapper.avgsqtime.values[i]:
        self.mapper.avgsqtime.slider.value = i

    for i in range(len(self.mapper.avgenergy.values)):
      if a.mapdesc.avgenergy == self.mapper.avgenergy.values[i]:
        self.mapper.avgenergy.slider.value = i

    for i in range(len(self.mapper.avgresources.values)):
      if a.mapdesc.avgresources == self.mapper.avgresources.values[i]:
        self.mapper.avgresources.slider.value = i

  def get(self):
    components = [archdesc.ComponentDesc("camera", 0, 0, float(self.network.bandwidth.label.text))]
  
    for c in self.network.components:
      if c.type.label.text != 'none':
        components.append(archdesc.ComponentDesc(c.type.label.text, 0, 0, float(c.bandwidth.label.text)))
      
    a = archdesc.ArchDesc(self.network.type.label.text, float(self.network.bandwidth.label.text), components, 1, self.network.type.values, self.network.bandwidth.values, self.network.components[0].type.values, self.network.components[0].bandwidth.values)
    m = archdesc.MapDesc(float(self.mapper.maxtime.label.text), float(self.mapper.avgtime.label.text),float(self.mapper.avgsqtime.label.text),float(self.mapper.avgenergy.label.text),float(self.mapper.avgresources.label.text))
    s = archdesc.SystemDesc(a, m)
    
    return s
